import Vue from 'vue'
import App from './App'

//下面两个是自定义头部导航栏必须的组件
import uniIcons from '@/components/uni-icons/uni-icons.vue'
Vue.component('uniIcons', uniIcons)
import hxNavbar from "@/components/hx-navbar/hx-navbar.vue"
Vue.component('hx-navbar', hxNavbar)
//end

// 没有任何数据图片
import listEmpty from "@/components/list-empty/index.vue"
Vue.component('list-empty', listEmpty)

// 屏幕底部定位
import fixedBottom from "@/components/fixed-bottom/index.vue"
Vue.component('fixed-bottom', fixedBottom)


// 异常页跳转
import pageEmpty from "@/components/page-empty/index.vue"
Vue.component('page-empty', pageEmpty)


//下面两个是上/下拉加载必须的组件
// import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"
import MescrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
// Vue.component('mescroll-body', MescrollBody)
Vue.component('mescroll-uni', MescrollUni)


Vue.config.productionTip = false

Vue.mixin({
	methods: {
		// showToast
		showToast(title, icon = 'none') {
			if(title==undefined) return;
			uni.showToast({
				title: title,
				icon: icon
			})
		},
		imgPrefix(url) { // 图片路径拼接
			return getApp().globalData.BASE_URL + url;
		}
	}
})
App.mpType = 'app'
const app = new Vue({
	...App
})
app.$mount()

/**
 * 方法说明
 * @method sceneDecode  微信scene解码
 * @return data 处理后的值
 */
Vue.prototype.sceneDecode = function (e) {
	// console.log(e)
	if (e === undefined)
		return {};
	let scene = decodeURIComponent(e),
		params = scene.split(','),
		data = {};
	for (let i in params) {
		var val = params[i].split(':');
		val.length > 0 && val[0] && (data[val[0]] = val[1] || null)
	}
	return data;
}
/**
   * 获取场景值(scene)
   */
Vue.prototype.getSceneData = function(query) { // 获取分享页面的参数，没有就直接获取页面普通参数
	console.log(query)
	return query.scene ? sceneDecode(query.scene) : query || {};
}



// 监听值变化
// this.observe(this.globalData, "tabBarType", this.watch);
// 上面这个是触发监听，一般放在ready/onshow/onlaunch
// this.watch是监听变化后触发
Vue.prototype.observe = function(obj, key, watch) {
	let val = obj[key];
	// const valtemp = obj[key]; // 备份一份原始数据出来，为了数据执行顺序考虑 
	Object.defineProperty(obj, key, {
		configurable: true,
		enumerable: true,
		set: function(value) {
			watch(val, value);
			val = value;
			// watch(valtemp, value);
		},
		get: function() {
			return val;
		}
	})
}


// 解决UNI-APP "请注意 showLoading 与 hideLoading 必须配对使用" 问题：https://blog.csdn.net/HRM2454/article/details/105969695/

/**
 * 方法说明
 * @method showLoading  加载信息，带遮罩
 * @return title  mask
 */
let needLoadingRequestCount = 0;
let loadingTimer;

Vue.prototype.showLoading = function(title = '', mask = true) {
	// console.log(title,9857612)
	if (needLoadingRequestCount === 0) {
		uni.showLoading({
			title,
			mask
		});
		// 最长3s自动关闭
		loadingTimer = setTimeout(() => {
			if (needLoadingRequestCount > 0) {
				uni.hideLoading();
			}
		}, 3000);
	}
	needLoadingRequestCount++;
}
 
// 隐藏遮罩
Vue.prototype.hideLoading= function(type=''){
	// console.log(type)
	if(type=='clear'){
		needLoadingRequestCount=1
	}
	if (needLoadingRequestCount <= 0) return;
	// console.log('执行了')
	needLoadingRequestCount--;
 
	if (needLoadingRequestCount === 0) {
	// console.log('清除了')
        loadingTimer && clearTimeout(loadingTimer);
		uni.hideLoading();
	}
}
